﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;

namespace CoastalClimbing.Auth
{
    public partial class Videos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindInfo();
            }
        }

        private void BindInfo()
        {
            grvVideos.DataSource = DAL.VideoSelect();
            grvVideos.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtTitle.Text) && !string.IsNullOrWhiteSpace(txtEmbeddedLink.Text))
            {
                btnAdd.Text = "Uploading...";
                btnAdd.Enabled = false;

                DAL.VideoCreate(txtTitle.Text, txtEmbeddedLink.Text, txtDescription.Text, Membership.GetUser().ProviderUserKey.ToString());

                this.BindInfo();

                txtTitle.Text = "";
                txtDescription.Text = "";
                txtEmbeddedLink.Text = "";
            }

            btnAdd.Text = "Submit";
            btnAdd.Enabled = true;
        }

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            grvVideos.EditIndex = -1;
            this.BindInfo();
        }

        protected void EditRecord(object sender, GridViewEditEventArgs e)
        {
            grvVideos.EditIndex = e.NewEditIndex;
            this.BindInfo();
        }

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            string id = grvVideos.DataKeys[e.RowIndex].Value.ToString();

            GridViewRow row = grvVideos.Rows[e.RowIndex];
            TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
            TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
            TextBox txtEmbeddedLink = (TextBox)row.FindControl("txtEmbeddedLink");

            try
            {
                DAL.VideoUpdate(id, txtTitle.Text, txtDescription.Text, Membership.GetUser().ProviderUserKey.ToString(), txtEmbeddedLink.Text);
                lblMessage.Text = "Record Updated Successfully.";
            }

            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            grvVideos.EditIndex = -1;
            this.BindInfo();
        }

        protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
        {
            string id = grvVideos.DataKeys[e.RowIndex].Value.ToString();

            try
            {
                DAL.VideoDelete(id);
                
                lblMessage.Text = "Record deleted Successfully.";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            this.BindInfo();

            //Response.Redirect("~/Auth/Videos.aspx");
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this video?')");
            }
        }
    }
}